import { getPlatform } from '@/api/user'

const state = {
  dialogLoading: false,
  projectInfo: sessionStorage.getItem("projectInfo") ? JSON.parse(sessionStorage.getItem("projectInfo")) : {
    title: '',
    promoter_platform: '1',
    logoUrl: ''
  }
};

const mutations = {
  SET_DIALOG_LOADING: (state, flag) => {
    state.dialogLoading = flag;
  },
  SET_PROJECTINFO: (state, data) => {
    const projectInfo = data.filter((item) => {
      if (location.href.indexOf(item.url) != -1) {
        item.title = item.label;
        item.promoter_platform = item.value
        return item
      }
    });
    const result = projectInfo.length > 0 ? projectInfo[0] : {
      ...data[0],
      title: data[0].label,
      promoter_platform: data[0].value,
      logoUrl: data[0].logoUrl
    };
    document.title = result.title +"后台管理系统"
    sessionStorage.setItem("projectInfo",JSON.stringify(result))
    state.projectInfo = result;
  },
};

const actions = {
  changeDialogLoading({ commit }, flag) {
    commit("SET_DIALOG_LOADING", flag);
  },
  getPlatFormList({ commit }) {
    getPlatform().then((res) => {
      commit('SET_PROJECTINFO', res)
    })
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
